package com.lvy.hczn.transmit.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.DictUtils;
import com.lvy.hczn.transmit.domain.dto.cteck.Container;
import com.lvy.hczn.transmit.service.ContainerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ContainerServiceImpl implements ContainerService {

    /**
     * 同步到平台
     *
     * @param list
     * @return void
     * @author yw
     * @date 2023-09-06 22:52:24
     */
    @Override
    public void syncContainerToPlatForm(List<Container> list) {
        if (list != null && !list.isEmpty()) {
            log.info("syncContainerToPlatForm同步到平台参数：{}", JSON.toJSONString(list));
            String domain = DictUtils.getDictValue(Constants.PLATFORM_DOMAIN, Constants.PLATFORM_DOMAIN_INTER);
            String url = DictUtils.getDictValue(Constants.INTER_TRANSMIT_PLATFORM_SYNC_INFO, Constants.INTER_TRANSMIT_WAREHOUSE_BOX_ADD);
            String codeResult = HttpRequest.post(domain + url).body(JSON.toJSONString(list)).execute().body();
            AjaxResult codeAjaxResult = JSONUtil.toBean(codeResult, AjaxResult.class);
            log.error("syncContainerToPlatForm同步到平台返回：" + JSONUtil.toJsonStr(codeAjaxResult));
            if (!String.valueOf(codeAjaxResult.get("isOk")).equals("1")) {
                //todo 异常处理，保存或重新发送
                throw new UtilException("syncContainerToPlatForm推送异常");
            }
        }
    }
}
